package no.saiboten.milkywave.controller;

import java.util.logging.Logger;

import no.saiboten.milkywave.graph.GraphService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DefaultController {

	private static final Logger LOGGER = Logger
			.getLogger(DefaultController.class.getName());

	GraphService graph;

	@Autowired
	public DefaultController(GraphService graph) {
		this.graph = graph;
	}

	@RequestMapping("/overview")
	public ModelAndView userOverView(
			@RequestParam(value = "result", required = false) String result) {
		ModelAndView mav = new ModelAndView("graph");
		if (result != null) {
			LOGGER.info("Result not null. Finding message for" + result);
			mav.addObject("result", findMessage(result));
		}

		mav.addObject("graph", graph.getGraphImg());

		return mav;
	}

	private String findMessage(String string) {
		if (string.equals("userdeleted"))
			return "Du er nå fjernet fra våre mailinglister!";
		if (string.equals("userconfirmed"))
			return "E-post bekreftet!!";
		if (string.equals("usercreated"))
			return "Bruker lagt til! Vennligst bekreft ditt medlemskap på e-post!";
		return null;
	}

}
